package pathways.SpeciesSpace;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import pathways.Database.Implementation.CDatabase;
import pathways.util.CUtil;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;

public class CSelectSpecies extends JDialog
{
  JLabel lblSpecies = new JLabel();
  JTextField txtSpecies = new JTextField();
  JScrollPane scrollPane = new JScrollPane();
  JList lstSpecies = new JList();
  JButton cmdOK = new JButton();
  JButton cmdCancel = new JButton();
  private CDatabase db;
  private CListModel lstmdl = null;
  private CItem selectedSpecies = null;
  
  public CSelectSpecies(JFrame owner, CDatabase database)
  {
    super(owner,"Select Species",true);
    Init(database);    
  }

  public CSelectSpecies(JDialog owner, CDatabase database)
  {
    super(owner,"Select Species",true);
    Init(database);    
  }
  
  private void Init(CDatabase database)
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    
    db = database;
    
    pack();
    setSize(280,270);    
  }
  
  private void jbInit() throws Exception
  {
    lblSpecies.setText("Species:");
    lblSpecies.setBounds(new Rectangle(10, 10, 60, 17));
    this.getContentPane().setLayout(null);
    txtSpecies.setBounds(new Rectangle(76, 10, 176, 21));
    txtSpecies.addKeyListener(new java.awt.event.KeyAdapter()
    {
      public void keyReleased(KeyEvent e)
      {
        txtSpecies_keyReleased(e);
      }
    });
    scrollPane.setBounds(new Rectangle(76, 48, 177, 147));
    cmdOK.setText("OK");
    cmdOK.setBounds(new Rectangle(88, 208, 73, 28));
    cmdOK.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdOK_actionPerformed(e);
      }
    });
    cmdCancel.setText("Cancel");
    cmdCancel.setBounds(new Rectangle(170, 208, 82, 29));
    cmdCancel.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdCancel_actionPerformed(e);
      }
    });
    this.getContentPane().add(lblSpecies, null);
    this.getContentPane().add(txtSpecies, null);
    this.getContentPane().add(scrollPane, null);
    scrollPane.getViewport().add(lstSpecies, null);
    this.getContentPane().add(cmdOK, null);
    this.getContentPane().add(cmdCancel, null);
  }

  void txtSpecies_keyReleased(KeyEvent e)
  {
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
    {
      String name = this.txtSpecies.getText();
      
      ResultSet rs = db.getSelect().getSpeciesMatchingName(name);
      
      this.lstmdl = new CListModel(rs);
      
      this.lstSpecies.setModel(this.lstmdl);
    }
  }

  void cmdOK_actionPerformed(ActionEvent e)
  {
    int index = this.lstSpecies.getSelectedIndex();
     
    if (index >= 0)
    {
      this.selectedSpecies = this.lstmdl.getSpecies(index);
      this.hide();
    }
    else
    {
      JOptionPane.showMessageDialog(this,"Please select a species.","Warning",JOptionPane.WARNING_MESSAGE); 
    }
  }
  
  public CItem getSelectedSpecies()
  {
    return this.selectedSpecies; 
  }

  void cmdCancel_actionPerformed(ActionEvent e)
  {
    this.selectedSpecies = null;
    this.hide();
  }
}

class CListModel extends AbstractListModel
{  
  public CListModel(ResultSet r)
  {
    data = r; 
  }
  
  public void setData(ResultSet r)
  {
    data = r;  
  }
  
  public ResultSet getData()
  {
    return data;  
  }
  
  public Object getElementAt(int index)
  {
    try {
        data.absolute(index+1);
        return data.getString(2);
    } catch(SQLException e) {
      CDebug.println(this, "getElementAt() - Access to element # " + (index+1) + " failed. Exception: " + e.toString());
      return null;
    }
  }
  
  public int getSize()
  { 
    try {     
        return CUtil.getResultSetRowCount(data);
    } catch(SQLException e) {
      CDebug.println(this, "getSize(). Exception: " + e.toString());
      return 0;
    }
  }
  
  public CItem getSpecies(int index)
  {
    try{
      data.absolute(index + 1);
      return (new CItem(data.getInt(1),data.getString(2)));
    } catch(SQLException e) {
      CDebug.println(this, "getSpecies(). Exception: " + e.toString());
      return null;
    }
  }
  
  private ResultSet data;
}
